package com.qbw.app;

import android.os.Environment;
import android.text.TextUtils;

import com.qbw.spm.P;

import java.io.File;

/*
 * Created by QBW on 2019/7/19
 */
public class Config {

    public static final String KEY_CURRENT_ENVIRONMENT = "key_current_environment";
    private static final String URL_DEV = "//114.119.41.126:8901";
//    private static final String URL_CST = "//nmapp.magicyo.com:8903";
    private static final String URL_CST = "//120.78.172.229:28042";

    private static final String URL_RELEASE = "//nmapp.magicyo.com:8903";
    private static String FLAVOR = "";
    public static final String VER = "v1";

    public static void init(String flavor) {
        FLAVOR = flavor;
        if (isDeveloperChannel()) {
            String env = P.getString(KEY_CURRENT_ENVIRONMENT);
            if (TextUtils.isEmpty(env)) {
                setCurrentEnvironment(flavor);
            }
        }
    }

    public static boolean isDeveloperChannel() {
        //return FLAVOR.equals("dev") || FLAVOR.equals("cst") || FLAVOR.equals("dft");
        return FLAVOR.equals("dev") || FLAVOR.equals("cst");
    }

    public static boolean isHttpChannel() {
        return FLAVOR.equals("dev") || FLAVOR.equals("cst");
    }

    public static String getSchema() {
        if (isHttpChannel()) {
            return "http:";
        }
        return "http:";
    }

    public static String getUrl() {
        return getSchema() + getUrlAuthorityPath();
    }


    public static String getUrlAuthority() {
        return getCurrentUrlByEnvironment();
    }

    public static String getUrlAuthorityPath() {
        return getUrlAuthority();
    }

    public static final String ROOT_DIR = Environment.getExternalStorageDirectory()
                                                     .getAbsolutePath() + File.separator + "magicyo";

    public static boolean isGoogleChannel() {
        return BuildConfig.FLAVOR.equals("google");
    }

    public static void setCurrentEnvironment(String env) {
        P.putString(KEY_CURRENT_ENVIRONMENT, env);
    }

    private static String getCurrentUrlByEnvironment() {
        if (isDeveloperChannel()) {
            String env = P.getString(KEY_CURRENT_ENVIRONMENT, "");
            if ("dev".equals(env)) {
                return URL_DEV;
            } else if ("cst".equals(env)) {
                return URL_CST;
            } else if ("dft".equals(env)) {
                return URL_RELEASE;
            }
        }
        return URL_RELEASE;
    }
}
